<template>
  <view class="cart">
    <!-- 1 列表 -->
    <view class="cart-list">
      <view v-for="(item,index) in list" :key="item.goods_id" class="cart-item">
        <!-- 1 复选框 -->
        <view class="goods-chk">
          <u-checkbox :value="item.checked" shape="circle" active-color="red" @change="handleGoodsToggle(index)"></u-checkbox>
        </view>
        <!-- 2 图片 -->
        <view class="goods-img">
          <u-image width="191rpx" height="191rpx" :src="item.goods_small_logo"></u-image>
        </view>
        <!-- 3 信息 -->
        <view class="goods-info">
          <!-- 名称 -->
          <view class="goods-name u-line-2">{{item.goods_name}}</view>
          <!-- 价格和数量 -->
          <view class="goods-price-num">
            <view class="goods-price">￥{{item.goods_price}}</view>
            <view class="goods-num-tool">
              <view class="num-btn" @click="handleNumChange(index,-1)">-</view>
              <view class="goods-num">{{item.num}}</view>
              <view class="num-btn" @click="handleNumChange(index,1)">+</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-chk">
        <!-- 因为是复选框 所以事件是change 而不是click -->
        <u-checkbox :value="allCheck" shape="circle" active-color="red" @change="toggleAllCheck">全选</u-checkbox>
      </view>
      <view class="all-price"> 合计: <text>￥{{checkedCartPrice}}</text> </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="handleGoPay">去结算({{checkedCartNums}})</u-button>
      </view>
    </view>
  </view>
</template>

<script>
// 引入辅助函数
import {mapState,mapMutations,mapGetters } from 'vuex'
export default {
  computed:{
    // 使用辅助函数
    ...mapState('cart',['list']),
    ...mapGetters('cart',['checkedCartNums','checkedCartPrice','allCheck'])
  },
  methods: {
    ...mapMutations('cart', ['toggleGoodsCheck','toggleAllCheck','numChange','deleteGoods']),
    // 商品选中事件
    handleGoodsToggle(index) {
      // 传递index  提交mutations
      // console.log(index);
      this.toggleGoodsCheck(index);
    },
    // 编辑商品数量
   async handleNumChange(index,num){
      // console.log(index,num);
      // 传当前点击的商品的 下标 和数量
      // this.numChange({index,num})
      // num 区分 点击 + (1) 还是 -(-1)
      // this.list[index].num 表示当前商品的数量
      if(num === -1 && this.list[index].num === 1){
        // 弹出确认框（由uniapp 的 uniapi 提供） 询问是否删除该商品
        // 返回值是 数组类型 [错误信息，返回值]
        const [err,{confirm}] = await uni.showModal({
          title:'提示',
          content:'是否删除该商品'
        })
        // 如果点击了确认
        if(confirm){
          // 调用 mutations 的删除方法
          // console.log('调用 mutations 删除方法 '); // 数组删除元素  数组.splice(下标,1);
          this.deleteGoods(index)
        }
      }else{
        // 否则还是正常的数量编辑
        this.numChange({index,num})
      }
    },
    // 去结算 点击事件
    handleGoPay(){
      // 判断购物车里面有没有商品
      if(this.checkedCartNums){
        // 有商品 则可以跳转到支付页面
        uni.navigateTo({url: '/pages/pay/pay'})
      }else{
        // 购物车里面没有商品
        uni.showToast({
          title:'您还没有选购商品',
          icon:'none'
        })
      }
    }
  },
};
</script>

<style lang="scss">
.cart {
  padding-bottom: 111rpx;
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: #eb4450;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: space-between;
            .num-btn {
              border: 1rpx solid #ccc;
              border-radius: 50%;
              width: 40rpx;
              height: 40rpx;
              text-align: center;
              line-height: 40rpx;
            }
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;
    .all-chk {
    }

    .all-price {
      flex: 1;
      color: #666;
      text {
      }
    }

    .all-count {
    }
  }
}
</style>